package app.modules.security.persistence.dao;

import app.modules.security.dto.web.resource.WebResourceDTO;
import app.modules.security.persistence.mapper.PermissionResourceRelationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jasmine.framework.database.mybatisplus.dao.BaseEntityDAO;
import jasmine.security.rbac.model.SecPermissionResourceRelation;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class PermissionResourceRelationDAO
        extends BaseEntityDAO<PermissionResourceRelationMapper, SecPermissionResourceRelation> {

    /**
     * 查询指定权限的所有已分配资源
     *
     * @param permissionId
     * @return
     */
    public List<WebResourceDTO> listAssignedResources(Long permissionId) {
        return baseMapper.listAssignedResources(permissionId);
    }

    /**
     * 删除指定权限的所有资源分配关系
     *
     * @param permissionId
     */
    public void deleteByPermissionId(Long permissionId) {
        LambdaQueryWrapper<SecPermissionResourceRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecPermissionResourceRelation::getPermissionId, permissionId);

        baseMapper.delete(wrapper);
    }

    /**
     * 删除指定资源的所有权限分配关系
     *
     * @param resourceId
     */
    public void deleteByResourceId(Long resourceId) {
        LambdaQueryWrapper<SecPermissionResourceRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecPermissionResourceRelation::getResourceId, resourceId);

        baseMapper.delete(wrapper);
    }

}
